<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2023/5/9
  Time: 15:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<head>
    <link rel="icon" href="<c:url value="/static/images/favicon.ico"/>">
    <link rel="stylesheet" href="<c:url value="/static/lib/layui-v2.5.5/css/layui.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/lib/font-awesome-4.7.0/css/font-awesome.min.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/layuimini.css?v=2.0.1"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/themes/default.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/public.css"/>" media="all">
    <title>添加文章</title>
</head>
<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">文章名</label>
            <div class="layui-input-block">
                <input type="text" name="aName" lay-verify="required" lay-reqtext="文章名不能为空" placeholder="请输入文章名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章描述</label>
            <div class="layui-input-block">
                <input type="text" name="aDesc" placeholder="请输入文章简介描述" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">文章内容</label>
            <div class="layui-input-block">
                <textarea type="text" name="aBody" lay-verify="required"
                          placeholder="请输入文章内容" value="" class="layui-input"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveArticleBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script src="<c:url value="/static/lib/layui-v2.5.5/layui.js"/>" charset="utf-8"></script>
<script src="<c:url value="/static/js/lay-config.js?v=2.0.0"/>" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;


        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        // 监听提交
        form.on('submit(saveArticleBtn)', function (data) {
            var user=parent.user;//访问父页面元素
            var articleVO = Object.assign(data.field,{authorId:user.userId});
            var index = layer.alert(JSON.stringify(articleVO), {
                title: '最终的提交信息'
            }, function () {
                $.ajax({
                    url: "/article/addArticle",
                    data: JSON.stringify({
                        'authorId': user.userId,
                        // 'articleId': -1,
                        'articleName': data.field.aName,
                        'articleDesc': data.field.aDesc,
                        'articleBody': data.field.aBody
                    }),
                    contentType: "application/json;charset=UTF-8",
                    dataType: "json",
                    type: "POST",
                    success:function(res){
                        if(res.status===200){
                            var seIndex=layer.alert(
                                res.message,
                                {
                                    title:'提示信息'
                                },
                                function (){
                                    layer.close(seIndex);
                                });
                        }else{
                            layer.msg(res.message,{icon:0,time:1000});
                            layer.close(index);
                        }
                        // 关闭弹出层
                       /* layer.close(parentIndex);*/
                    },
                    error:function(e){
                        console.log(e,e.message);
                        alert('error');
                    }
                });

            });


            return false;
        });

    });
</script>